<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
      <el-button class="btn-add" icon="el-icon-delete" size="mini" @click="batchRemove()">批量删除</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="image" label="图片" width="120px">
        <template slot-scope="scope">
          <img :src="scope.row.image" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="影视名称" />
      <el-table-column prop="director" label="导演" />
      <el-table-column prop="actor" label="演员" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" title="修改" @click="edit(scope.row.id)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除" @click="removeDataById(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
    <!-- 添加的弹窗 -->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="movie" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="影视名称">
          <el-input v-model="movie.name" />
        </el-form-item>
        <el-form-item label="所属栏目">

          <select v-model="movie.id">
            <option value="1">电视剧</option>
            <option value="2">电影</option>
            <option value="3">综艺</option>
            <option value="4">动漫</option>
            <option value="5">网剧</option>
            <option value="6">纪录片</option>
          </select>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input v-model="movie.keyword" />
        </el-form-item>
        <el-form-item label="导演">
          <el-input v-model="movie.director" />
        </el-form-item>
        <el-form-item label="演员">
          <el-input v-model="movie.actor" />
        </el-form-item>
        <el-form-item label="影视图片">
          <el-upload
            class="upload-demo"
            name="uploadImage"
            action="dev-api/admin/upload/image"
            :on-success="handleImageSuccess"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="影视文件">
          <el-upload
            class="upload-demo"
            drag
            name="uploadVideo"
            action="dev-api/admin/upload/video"
            :on-success="handleVideoSuccess"
            multiple
          >
            <i class="el-icon-upload" />
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div slot="tip" class="el-upload__tip">只能上传视频文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="影视描述">
          <el-input v-model="movie.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-refresh-right" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" size="small" @click="saveOrUpdate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api/movie/movie.js'
export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      limit: 3,
      listLoading: false,
      dialogVisible: false,
      movie: {},
      selectValueArr: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 刷新页
    fetchData(page = 1) {
      this.page = page
      api.getPageList(this.page, this.limit).then(response => {
        console.log(response)
        this.list = response.data.records
        this.total = response.data.total
      })
    },
    // 进入添加
    add() {
      this.dialogVisible = true
      // 打开编辑框
      this.movie = {}
      // 对绑定对象进行清空
    },
    // 进入保存数据
    saveOrUpdate() {
      // 保存的功能有以下判断
      if (this.movie.id != null) {
        this.updateMovie()
      } else {
        this.addMovie()
      }
    },
    // 添加数据
    addMovie() {
      api.saveMovie(this.movie).then(response => {
        // 判断是否成功
        if (response.code === 200) {
          this.dialogVisible = false
          // 添加弹窗
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          // 刷新列表
          this.fetchData()
        }
      })
    },
    // 修改数据
    updateMovie() {
      api.updateMovie(this.movie).then(response => {
        // 判断是否成功
        if (response.code === 200) {
          this.dialogVisible = false
          // 添加弹窗
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          // 刷新列表
          this.fetchData()
        }
      })
    },
    // 进入编辑界面
    edit(id) {
      api.getMovieById(id)
        .then((response) => {
          if (response.code === 200) {
            console.log(response)
            this.movie = response.data
            this.dialogVisible = true
          }
        })
    },
    // 删除用户
    removeDataById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.removeId(id)
          .then((response) => {
            if (response.code === 200) {
              // 提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              // 刷新页面
              this.fetchData()
            }
          })
      })
    },
    // 把批量删除的数据转换
    handleSelectionChange(selectValue) {
    //   console.log(selectValue)
      this.selectValueArr = selectValue
    },
    // 进行批量删除操作
    batchRemove() {
      if (this.selectValueArr.length === 0) {
        this.$message.warning('没有选择角色')
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var ids = []
        for (var i = 0; i < this.selectValueArr.length; i++) {
          var obj = this.selectValueArr[i]
          // 获取id值
          var id = obj.id
          // 将id放进到数组中
          ids.push(id)
        }
        api.bactchremoveId(ids)
          .then((response) => {
            if (response.code === 200) {
              // 提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              // 刷新页面
              this.fetchData()
            }
          })
      })
    },
    // 上传图片的函数
    handleImageSuccess(file, res) {
      console.log(file)
      console.log(res)

      if (res.status === 'success') {
        // 提示
        this.$message({
          type: 'success',
          message: '上传成功!'
        })
        this.movie.image = file
      }
    },
    // 上传影视的函数
    handleVideoSuccess(file, res) {
      console.log(file)
      console.log(res)

      if (res.status === 'success') {
        // 提示
        this.$message({
          type: 'success',
          message: '上传成功!'
        })
        this.movie.play_id = file
      }
    }
  }
}
</script>

<style>

</style>
